# import pymysql
# conn = pymysql.connect(host = 'localhost', user='root', password= 'root', database= 'advanced', port=3306, charset='utf8')
# cursor = conn.cursor()
#
# try:
#     sql = "INSERT INTO student(stu_id, stu_name) VALUES (8, '许嵩')"
#     rows = cursor.execute(sql)
#     print('受影响行数%d' % rows)
# # except Exception as e:
# #     print(e)
# #     conn.rollback()
# # else:
# #     conn.commit()
# # cursor.close()
# # conn.close()
# # 导入pymysql
# import pymysql
# #创建连接对象
# conn = pymysql.connect(user='root', password='root', database='advanced', port=3306, charset='utf8')
# # TODO 3.获取游标对象
# cursor = conn.cursor()
# # TODO 4.执行sql语句
# # TODO 5.提交或者回滚
# try:
#     sql = "insert into student(stu_id, stu_name) values(8, '许嵩')"
#     rows = cursor.execute(sql) # 返回受影响行数
#     print('受影响行%d' % rows)
# except Exception as e:
#     print(e)
#     conn.rollback()
# else:
#     conn.commit()
#     print('提交成功')
# cursor.close()
# conn.close()
# import pymysql
# # ""
# # 通过用户名获取用户信息
# # """
# # # 　创建连接对象和游标对象
# conn = pymysql.connect(user='root', password='root', database='advanced', charset='utf8')
# cursor = conn.cursor()
# # 　执行sql语句
# name = input('姓名:')
# sql = f"select * from student where stu_name = '{name}'"
# print(sql)
# rows = cursor.execute(sql)
# if rows:
#     students = cursor.fetchall()
#     print(students)
# else:
#     print('没有要找的学生')
# cursor.close()
# # conn.close()
# import pymysql
#
# """
# 通过用户名获取用户信息
# """
# # 　创建连接对象和游标对象
# conn = pymysql.connect(user='root', password='root', database='advanced', charset='utf8')
# cursor = conn.cursor()
# # 　执行sql语句
# name = input('姓名:')
# sql = "SELECT * FROM student WHERE stu_name = %s"
# print(sql)
# rows = cursor.execute(sql, [name])
# if rows:
#     # 获取结果集
#     students = cursor.fetchall()
#     print(students)
# else:
#     print('没有要找的学生')
import pymysql

"""
通过用户名或者年龄获取用户信息
"""
# 　创建连接对象和游标对象
conn = pymysql.connect(user='root', password='root', database='advanced', charset='utf8')
cursor = conn.cursor()

# 　执行sql语句
name = input('姓名:')
age = int(input('年龄:'))
# name = ' or 1=1 or '
sql = "SELECT * FROM student WHERE stu_name = %s or stu_age = %s"
print(sql)
rows = cursor.execute(sql, [name, age])
if rows:
    # 获取结果集
    students = cursor.fetchall()
    print(students)
else:
    print('没有要找的学生')
# 关闭资源
cursor.close()
conn.close()









